package sgi.server;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import java.awt.ScrollPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JTextField;
import javax.swing.JButton;

public class SGIServerUI extends JFrame {

	private JPanel contentPane;
	private JTextArea serverConsole;
	private JLabel lblDbName;
	private JTextField textFieldDBName;
	private JLabel lblPort;
	private JTextField textFieldPort;
	private JButton btnConnect;
	private JTextField textFieldUser;
	private JLabel lblUsername;
	private JLabel lblPassword;
	private JPasswordField textFieldPass;
	SGIServer server;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame.
	 */
	public SGIServerUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setTitle("SGI Server");
		contentPane = new JPanel();
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 71, 414, 180);
		contentPane.add(scrollPane);
		
		serverConsole = new JTextArea();
		scrollPane.setViewportView(serverConsole);
		serverConsole.setEditable(false);
		
		lblDbName = new JLabel("DB Name:");
		lblDbName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDbName.setBounds(10, 11, 81, 14);
		contentPane.add(lblDbName);
		
		textFieldDBName = new JTextField();
		textFieldDBName.setBounds(76, 10, 86, 20);
		contentPane.add(textFieldDBName);
		textFieldDBName.setColumns(10);
		
		lblPort = new JLabel("Port:");
		lblPort.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPort.setBounds(167, 16, 46, 14);
		contentPane.add(lblPort);
		
		textFieldPort = new JTextField();
		textFieldPort.setBounds(220, 10, 86, 20);
		contentPane.add(textFieldPort);
		textFieldPort.setColumns(10);
		
		btnConnect = new JButton("Connect");
		btnConnect.setBounds(316, 9, 89, 23);
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					server.setPort(Integer.valueOf(textFieldPort.getText()));
					  try 
						{
				            Class.forName("com.mysql.jdbc.Driver").newInstance();
				        	server.initDBConnection(textFieldDBName.getText(),textFieldUser.getText(),new String(textFieldPass.getPassword()));
				        	display("SQL connection succeed");
				     	}  catch (Exception e1) {display("SQL connection failed.");}
					  
					  try
					  	{
						  	server.listen(); //Start listening for connections
						  	display("Server is listening on port " + textFieldPort.getText());
					  	} catch (Exception e1) {display("ERROR - Could not listen for clients!");}
			}
		});
		contentPane.add(btnConnect);
		
		textFieldUser = new JTextField();
		textFieldUser.setBounds(76, 40, 86, 20);
		contentPane.add(textFieldUser);
		textFieldUser.setColumns(10);
		
		lblUsername = new JLabel("Username:");
		lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblUsername.setBounds(10, 46, 64, 14);
		contentPane.add(lblUsername);
		
		lblPassword = new JLabel("Password:");
		lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblPassword.setBounds(167, 46, 64, 14);
		contentPane.add(lblPassword);
		
		textFieldPass = new JPasswordField();
		textFieldPass.setBounds(220, 40, 86, 20);
		contentPane.add(textFieldPass);
		textFieldPass.setColumns(10);
	}
	
	public void display(String s)
	{
		serverConsole.append(s + "\n");
	}
	
	public void setServer(SGIServer server)
	{
		this.server = server;
	}
	
	public static void main(String[] args) {
	    
	    SGIServerUI serverui = new SGIServerUI();
	    SGIServer server = new SGIServer(serverui);
	    serverui.setServer(server);
	}
}
